package com.service.zy;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.controller.zy.PageBean;
import com.dao.zy.ProductClassifyDao;
import com.dao.zy.ProductDao;
import com.dao.zy.ProductStandardDao;
import com.dao.zy.ProductStandardMybatisDao;
import com.entity.Product;
import com.entity.ProductClassify;
import com.entity.ProductStandard;
import com.entity.Storage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.ServiceException;

@Service
@Transactional(rollbackFor=ServiceException.class)
public class ProductStandardService {
	@Autowired
	private ProductStandardDao productstandardDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductStandardMybatisDao productstandardmybatisDao;
	@Autowired
	private ProductClassifyDao productclassifyDao;
	
	/**
	 * 增加或修改一个产品规格
	 * @param ps
	 * @throws ServiceException
	 */
	public void addOne(ProductStandard ps) throws ServiceException {
		try {
			Product pro = productDao.findOne(ps.getProduct().getId());
			ps.setProduct(pro);
			productstandardDao.save(ps);
		}catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 删除一个产品规格
	 * @param id
	 * @throws ServiceException 
	 */
	public String delOne(Long stanid) throws ServiceException {
		try {
			ProductStandard ps = productstandardDao.findOne(stanid);
			System.out.println(ps.getName());
			if(ps.getName().equals("基准")) {
//				System.out.println("这是基准规格");
				List<ProductStandard> list = productstandardmybatisDao.queryByProId(ps.getProduct().getId());
				int num = list.size()-1;
//				System.out.println("总规格数"+num);
				if(num>0) {
					return "产品含有"+num+"个其它规格产品，无法删除！";
				}else {
					productstandardDao.delete(stanid);
					productDao.delete(ps.getProduct().getId());
					return "删除成功";
				}
			}else {
				productstandardDao.delete(stanid);
				return "删除成功";
			}
		}catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 查询一个产品规格
	 * @param standard_id
	 * @return
	 */
	public ProductStandard findOne(Long standard_id){
		ProductStandard ps = productstandardDao.findOne(standard_id);
		Product pro = productDao.findOne(ps.getProduct().getId());
		ps.setProduct(pro);
		List<Storage> ss = ps.getStorages();
		ss.size();
		return ps;
	}
	/**
	 * 查询一个产品的所有规格
	 * @param product
	 * @return
	 */
	public List<ProductStandard> findByPro(Long id){
		return productstandardmybatisDao.queryByProId(id);
	}
	/**
	 * 查询所有产品规格
	 * @return
	 */
	public List<ProductStandard> findAll(){
		return (List<ProductStandard>) productstandardDao.findAll();
	}
	/**
	 * 产品规格的高级查询
	 * @param productstandard
	 * @return
	 */
	public List<ProductStandard> findByTrim(ProductStandard productstandard){
		System.out.println("着了"+productstandard.getName());
		List<ProductStandard> list = productstandardmybatisDao.findByTrim(productstandard);
		ProductStandard ps = productstandardDao.findOne(list.get(0).getStanid());
		Product pro = productDao.findOne(ps.getProduct().getId());
		for(int i=0;i<list.size();i++) {
			System.out.println("类别集合长度"+list.size());
			list.get(i).setProduct(pro);
		}
		return list;
	}
	
	/**
	 * 根据类别查询
	 * @param classifyId
	 * @return
	 */
	public List<ProductStandard> findByClassify(Long classifyId){
		List<ProductStandard> list = productstandardmybatisDao.findByClassify(classifyId);
		ProductStandard ps = productstandardDao.findOne(list.get(0).getStanid());
		Product pro = productDao.findOne(ps.getProduct().getId());
		for(int i=0;i<list.size();i++) {
			System.out.println("类别集合长度"+list.size());
			list.get(i).setProduct(pro);
//			ProductStandard ps = list.get(i);
//			if(ps.getProduct()!=null&&ps.getProduct().getId()!=null) {
//				Product pro = productDao.findOne(list.get(i).getProduct().getId());
//				ProductClassify pc = productclassifyDao.findOne(classifyId);
//				pro.setProductClassify(pc);
//				ps.setProduct(pro);
//			}
		}
		return list;
	}
	
	public PageBean findByPage(int pageCode, int pageSize) {
	    //使用Mybatis分页插件
	    PageHelper.startPage(pageCode,pageSize);
	    //调用分页查询方法，其实就是查询所有数据，mybatis自动帮我们进行分页计算
	    Page<ProductStandard> page = productstandardmybatisDao.findByPage();
	    return new PageBean(page.getTotal(),page.getResult());
	}
}
